package com.fulihui.common.builder.rpc;

import com.fulihui.common.error.Errors;
import com.fulihui.common.error.ServiceError;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.PageResult;
import com.fulihui.common.result.rpc.SimpleResult;


/**
 * 用户rpc结果返回构建
 *
 * @author: Levon
 * @version: v 0.1 2018-06-14 14:50
 */
public class RpcResultBuilder {

    public static BaseResult succ() {
        BaseResult rs = new BaseResult();
        rs.setErrcode(BaseResult.SUCCESS);
        rs.setErrmsg(BaseResult.SUCC_MSG);
        return rs;
    }

    @SuppressWarnings("unchecked")
    public static <T extends BaseResult> T succ(Class<T> clz) {
        try {
            BaseResult rs = clz.newInstance();
            rs.setErrcode(BaseResult.SUCCESS);
            rs.setErrmsg(BaseResult.SUCC_MSG);
            return (T) rs;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T extends PageResult> T succPageResult(Class<T> clz, int page, int rows,
                                                          int totalCount) {
        try {
            PageResult rs = clz.newInstance();
            rs.setPage(page);
            rs.setRows(rows);
            int totalPage = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
            rs.setTotalPage(totalPage > 0 ? totalPage : 1);
            rs.setTotalRows(totalCount);
            rs.setErrcode(BaseResult.SUCCESS);
            rs.setErrmsg(BaseResult.SUCC_MSG);
            return (T) rs;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static BaseResult fail(int errcode, String errmsg) {
        BaseResult rs = new BaseResult();
        rs.setErrcode(errcode);
        rs.setErrmsg(errmsg);
        return rs;
    }

    public static BaseResult fail(ServiceError err) {
        if (err == null) {
            err = Errors.Commons.SYSTEM_ERROR;
        }
        return fail(err.getErrcode(), err.getErrmsg());
    }

    public static <T extends BaseResult> T fail(Class<T> clz, ServiceError error) {
        if (error == null) {
            error = Errors.Commons.SYSTEM_ERROR;
        }
        return fail(clz, error.getErrcode(), error.getErrmsg());
    }

    @SuppressWarnings("unchecked")
    public static <T extends BaseResult> T fail(Class<T> clz, int errcode, String errmsg) {
        try {
            BaseResult rs = clz.newInstance();
            rs.setErrcode(errcode);
            rs.setErrmsg(errmsg);
            return (T) rs;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> SimpleResult<T> succSimple(T data) {
        SimpleResult rs = new SimpleResult();
        rs.setErrcode(BaseResult.SUCCESS);
        rs.setErrmsg(BaseResult.SUCC_MSG);
        rs.setData(data);
        return rs;
    }

    public static <T> SimpleResult<T> failSimple(ServiceError error) {
        if (error == null) {
            error = Errors.Commons.SYSTEM_ERROR;
        }
        return failSimple(null, error.getErrcode(), error.getErrmsg());
    }

    public static <T> SimpleResult<T> failSimple(T data, int errcode, String errmsg) {
        try {
            SimpleResult rs = new SimpleResult();
            rs.setErrcode(errcode);
            rs.setErrmsg(errmsg);
            rs.setData(data);
            return rs;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
